<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Labor;
use App\Models\LaborTeam;
use App\Models\ResourceBind;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaborTeamService
{
    public function get($title)
    {
        $sql = LaborTeam::when($title, function ($query, $title) {
            return $query->where(function($query) use ($title) {
                $query->where('name','like', '%'.$title.'%');
            });
        });
        return $sql;
    }

    public function insert($input){
        if($input['birthday']){
            $input['birthday'] = Date("Y-m-d",$input['birthday'] / 1000);
        }
        // if ($input['in_time']) {
        //     $input['in_time'] = Date("H:i:s", $input['in_time'] / 1000);
        // }
        // if ($input['out_time']) {
        //     $input['out_time'] = Date("H:i:s", $input['out_time'] / 1000);
        // }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }

        try {
            $data = DB::transaction(function () use ($input,$res,$files) {
                if($input['director'] && $input['contact_mobile']){
                    $is = Labor::where('company_id',$input['company_id'])->where('mobile',$input['contact_mobile'])->where('id_card',$input['id_card'])->first();
                    if(!$is){
                        $Labor = new Labor();
                        $Labor->name = $input['director'];
                        $Labor->mobile = $input['contact_mobile'];
                        $Labor->company_id = $input['company_id'];
                        $Labor->id_card = $input['id_card'];
                        $Labor->save();
                    }
                }

                $data = LaborTeam::create($res);

                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $data->id;;
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'LaborTeam';
                    $bind_insert[] = $bind;
                }
                ResourceBind::insert($bind_insert);
                //添加到人员库存
                if($data){
                    return $data->id;
                }
            });

            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        if($input['birthday']){
            $input['birthday'] = Date("Y-m-d",$input['birthday'] / 1000);
        }
        // if ($input['in_time']) {
        //     $input['in_time'] = Date("H:i:s", $input['in_time'] / 1000);
        // }
        // if ($input['out_time']) {
        //     $input['out_time'] = Date("H:i:s", $input['out_time'] / 1000);
        // }
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }

        $files = [];
        if(!empty($res['files'])){
            $files = explode(',',$res['files']);
        }

        unset($res['id']);
        try {
            $data = DB::transaction(function () use ($input,$res,$files) {
                if($input['director'] && $input['contact_mobile']){
                    $is = Labor::where('company_id',$input['company_id'])->where('mobile',$input['contact_mobile'])->where('id_card',$input['id_card'])->first();
                    if(!$is){
                        $Labor = new Labor();
                        $Labor->name = $input['director'];
                        $Labor->mobile = $input['contact_mobile'];
                        $Labor->company_id = $input['company_id'];
                        $Labor->id_card = $input['id_card'];
                        $Labor->save();
                    }
                }

                $data = LaborTeam::where('id',$input['id'])->update($res);

                $bind_insert = [];
                foreach ($files as $k => $v) {
                    $bind = [];
                    $bind['source_id'] = $input['id'];
                    $bind['resource_id'] = $v;
                    $bind['model'] = 'LaborTeam';
                    $bind_insert[] = $bind;
                }
                ResourceBind::where('source_id',$input['id'])->where('model','LaborTeam')->delete();
                if(count($bind_insert)){
                    ResourceBind::insert($bind_insert);
                }

                if($data){
                    return $input['id'];
                }
            });

            return $data;

        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

}